package com.ldn.dao.entities;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import com.ldn.basic.utils.DataUtil;
import com.ldn.basic.utils.DateUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 李大牛家的牛 (scafel@163.com) 
 * @createDate 2024/7/11 - 13:28
 * @description 基础实体类
 **/

@Data
@Schema(name="提出实体类的公共字段", description = "提出实体类的公共字段")
public class BasicEntities {

    @Id(keyType = KeyType.Auto)
    @JsonIgnore
    @Schema(description = "主键ID")
    protected Integer id;
    @Schema(description = "唯一编码")
    @Column(value = "code")
    @JsonProperty(value = "code")
    protected String code;
    @Schema(description = "排序", defaultValue = "0")
    @Column(value = "sort")
    @JsonProperty(value = "sort")
    private long sort;
    @Schema(description = "字段状态", defaultValue = "1")
    @Column(value = "status")
    @JsonProperty(value = "status")
    protected long status;
    @Schema(description = "受保护的状态", defaultValue = "0")
    @Column(value = "is_protected")
    @JsonProperty(value = "is_protected")
    protected long isProtected;
    @Schema(description = "热度标识符", defaultValue = "0")
    @Column(value = "is_hot")
    @JsonProperty(value = "is_hot")
    protected long isHot;
    @Schema(description = "表乐观锁版本", defaultValue = "0")
    @JsonIgnore
    @Column(value = "version", version = true)
    @JsonProperty(value = "version")
    protected long version;
    @Schema(description = "伪删除", defaultValue = "0")
    @Column(value = "is_delete", isLogicDelete = true)
    @JsonProperty(value = "is_delete")
    @JsonIgnore
    protected long isDelete;
    @Schema(description = "创建时间时间戳", defaultValue = "0")
    @Column(value = "c_time")
    @JsonProperty(value = "c_time")
    @JsonIgnore
    protected long ctime;
    @Schema(description = "删除时间时间戳", defaultValue = "0")
    @JsonIgnore
    @Column(value = "d_time")
    @JsonProperty(value = "d_time")
    protected long dtime;
    @Schema(description = "更新时间时间戳", defaultValue = "0")
    @Column(value = "u_time")
    @JsonProperty(value = "u_time")
    @JsonIgnore
    protected long utime;
    @Schema(description = "更新时操作系统", defaultValue = "''")
    @Column(value = "u_os")
    @JsonProperty(value = "u_os")
    protected String uOs;
    @Schema(description = "更新时操作IP", defaultValue = "''")
    @Column(value = "u_ip")
    @JsonProperty(value = "u_ip")
    protected String uIp;
    @Schema(description = "创建时管理员ID", defaultValue = "0")
    @Column(value = "c_a_u_id")
    @JsonProperty(value = "c_a_u_id")
    protected Integer cauId;
    @Schema(description = "更新时管理员ID", defaultValue = "0")
    @Column(value = "u_a_u_id")
    @JsonProperty(value = "u_a_u_id")
    protected Integer uauId;
    @Schema(description = "创建时会员ID", defaultValue = "0")
    @Column(value = "c_m_u_id")
    @JsonProperty(value = "c_m_u_id")
    protected Integer cmuId;
    @Schema(description = "更新时会员ID", defaultValue = "0")
    @Column(value = "u_m_u_id")
    @JsonProperty(value = "u_m_u_id")
    protected Integer umuId;
    @Schema(description = "ID的加密字符串")
    @Column(ignore = true)
    protected String enId;
    @Schema(description = "创建时间时间")
    @JsonProperty(value = "c_time_value")
    @Column(ignore = true)
    protected String ctimeValue;
    @Schema(description = "更新时间时间")
    @JsonProperty(value = "u_time_value")
    @Column(ignore = true)
    protected String utimeValue;
    @Schema(description = "删除时间时间")
    @JsonProperty(value = "d_time_value")
    @Column(ignore = true)
    protected String dtimeValue;

    public void setEnId() {
        this.enId = DataUtil.en(String.valueOf(getId()));
    }

    public String getEnId() {
        return DataUtil.en(String.valueOf(getId()));
    }

    public String[] getEnIds(String idStr) {
        if (StrUtil.isBlank(idStr)) {
            return new String[0];
        }
        String[] ids = idStr.split(",");
        if (ids.length == 0){
            return new String[0];
        }
        List<String> res = new ArrayList<>();
        for (String id : ids) {
            if (StrUtil.isNotBlank(id)){
                res.add(DataUtil.en(id));
            }
        }
        return res.toArray(new String[0]);
    }

    public String getCtimeValue() {
        return DateUtils.formatDate(ctime);
    }
    public String getUtimeValue() {
        return DateUtils.formatDate(utime);
    }
    public String getDtimeValue() {
        return DateUtils.formatDate(dtime);
    }

}
